package com.wangsd.web.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.coupon.entity.IntegralDetail;
import com.wangsd.web.coupon.entity.Member;
import com.wangsd.web.coupon.entity.Merchant;
import com.wangsd.web.coupon.mapper.MemberMapper;
import com.wangsd.web.coupon.service.IntegralDetailService;
import com.wangsd.web.coupon.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-09-25
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Resource
    MemberMapper memberMapper;
    @Autowired
    IntegralDetailService integralDetailService;

    @Override
    public Member selectByOpenid(String openid) {
        return memberMapper.selectOne(new QueryWrapper<Member>().eq("mini_openid", openid));
    }

    @Override
    public Member selectByPayOpenid(String openid) {
        return memberMapper.selectOne(new QueryWrapper<Member>().eq("pay_openid", openid));
    }

    @Override
    public Member selectByOpenid2(String openid, Long companyId) {
        QueryWrapper queryWrapper = new QueryWrapper<Member>();
        queryWrapper.eq("pay_openid", openid);
        queryWrapper.eq("company_id",companyId);
        return memberMapper.selectOne(queryWrapper);
    }

    @Override
    public void register(Member member) {
        Member old = selectByOpenid(member.getMiniOpenid());
        if (old != null) {
            /*当会员中保存头像或者手机号是，授权成认证会员*/
            if (old.getAvatarUrl() != null && member.getMobile() != null) {
                member.setStatus(1);
            } else if (member.getAvatarUrl() != null && old.getMobile() != null) {
                member.setStatus(1);
            }
            member.setId(old.getId());
            memberMapper.updateById(member);
        } else {
            memberMapper.insert(member);
        }
    }

    @Override
    public Member selectByCompanyIdAndPhone(String phone, Long companyId) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",phone).eq("company_id",companyId);
        return memberMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional
    public void updateIntegralByOpenid(String openid) {
        memberMapper.updateIntegralByOpenid(openid);
    }

    @Override
    public void updateReduceIntegralByOpenid(String openid) {
        memberMapper.updateReduceIntegralByOpenid(openid);
    }

    @Override
    public void updateIntegral(String openid, Integer points) {
        memberMapper.updateIntegral(openid,points);
    }

    @Override
    public IPage<Member> selectPage(String code, Integer pageNum, Integer pageSize, Member member) {
        Page<Member> page = new Page<>(pageNum, pageSize);
        IPage<Member> resultIPage = memberMapper.selectListXml(page, member, code);
        return resultIPage;
    }

    @Override
    public int dealZeroPoints(Member params) {
        List<Member> members = memberMapper.selectList(new QueryWrapper<Member>().eq("company_id",params.getCompanyId()));

        /*批量新增到记录*/
        List<IntegralDetail> list = new ArrayList<>();
        members.forEach(member -> {
            IntegralDetail integralDetail = new IntegralDetail();
            integralDetail.setCreatedTime(new Date());
            integralDetail.setOpenId(member.getMiniOpenid());
            integralDetail.setIntegral(member.getIntegral());
            /*扣积分*/
            integralDetail.setStatus(0);
            /*pc端扣除积分*/
            integralDetail.setType(4);
            integralDetail.setRemark(params.getRemark());
            list.add(integralDetail);
        });
        integralDetailService.saveBatch(list);

        /*交易表中生成记录*/
        //trade.setTotalFee(member.getIntegral()+"");
        //tradeService.save(trade);

        /*整个商圈批量积分清零*/
        int i = memberMapper.dealZeroPointsByCompanyId(params.getCompanyId(), 0);
        return i;
    }
}
